<?php
class BIS_Mailer extends Zend_Mail
{
    /**
     *
     * @var Zend_View
     */
    static $_defaultView;

    /**
     * current instance of our Zend_View
     * @var Zend_View
     */
    protected $_view;

    protected static function getDefaultView()
    {
        if(self::$_defaultView === null)
        {
            self::$_defaultView = new Zend_View();
            self::$_defaultView->setScriptPath(APPLICATION_PATH .
                    '/views/scripts/mails');
        }
        return self::$_defaultView;
    }

    public function sendHtml($template, $encoding = Zend_Mime::ENCODING_QUOTEDPRINTABLE)
    {
        $html = $this->_view->render($template);
        $this->setBodyHtml($html,$this->getCharset(), $encoding);
        $this->send();
    }

    public function setViewParam($property, $value)
    {
        $this->_view->__set($property, $value);
        return $this;
    }

    public function __construct($systemSetting = null, $charset = 'iso-8859-1')
    {
        if(null !== $systemSetting){

            if('mail' == $systemSetting->mailProtocol){

                $transport = new Zend_Mail_Transport_Sendmail($systemSetting->mailParameters);

            }elseif('smtp' == $systemSetting->mailProtocol){

                $config = array('auth' => 'login',
                                'username' => $systemSetting->smtpUsername,
                                'password' => $systemSetting->smtpPassword,
                                'ssl' => $systemSetting->smtpConnection,
                                'port' => $systemSetting->smtpPort,
                                'timeout' => $systemSetting->smtpTimeout);

                $transport = new Zend_Mail_Transport_Smtp($systemSetting->smtpHost, $config);

            }
            parent::setDefaultTransport($transport);
        }
        parent::__construct($charset);
        $this->_view = self::getDefaultView();
    }
}

